/**
 * Represents a multimedia content (Video and Photo)
 * 
 * @author remo.lemma@lu.unisi.ch and patrick.zulian@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.elements;

import ch.unisi.inf.projects.java.uom.Database;

public abstract class Multimedia
{
	/** The id of the entry */
	private String id;
	
	/** The title of this multimedia content */
	private String title;
	
	/** The URL */
	private final String URL;
	
	/** Comment for this content */
	private String comment;
	
	/** The type of the content */
	private final String type;
	
	/** Boolean that is true if the content has been modified (needs to be updatetd) */
	private boolean modified;
	
	/**
	 * Constructor
	 * @param title the title
	 * @param URL the URL
	 * @param comment the comment
	 * @param type the type
	 */
	public Multimedia (final String title, final String URL, final String comment, final String type)
	{
		this("", title, URL, comment, type);
		this.modified = true;
	}
	
	/**
	 * Constructor (with id field, should not be called from the user)
	 * @param id the id of the content
	 * @param title the title
	 * @param URL the URL
	 * @param comment the comment
	 * @param type the type
	 */
	public Multimedia (final String id, final String title, final String URL, final String comment, final String type)
	{
		this.id = id;
		this.title = title;
		this.URL = URL;
		this.comment = comment;
		this.type = type;
		this.modified = false;
	}
	
	/**
	 * Creates a multimedia subtye instance for the given item (and the given id)
	 * @param i the Item that represents the Multimedia
	 * @param id the id of the multimedia content
	 * @return the Multimedia instance;
	 */
	private static Multimedia instanceMultimedia (final Item i, final String id)
	{
		final String type = i.getField("STRING_TYPE");
		// TODO : Think better solution, but well only two types of multimedia...
		if (type.toUpperCase().equals("PHOTO")) {
			final Photo photo = new Photo(id, 
										  i.getField("STRING_NAME"),
										  i.getField("STRING_URL"),
										  i.getField("STRING_COMMENT"));
			return photo;
		}
		return null;
	}
	
	/**
	 * Returns a Multimedia instance for the given id
	 * @param id the id of the content
	 * @return A Multimedia instance
	 * @throws Exception handles the needed exceptions
	 */
	public static Multimedia getFromId (final String id) throws Exception
	{
		if (id == null || id.equals(""))
			return null;
		final ItemClass itemClass = ItemManager.instance().itemClassGet("Multimedia");
		if (itemClass == null)
			return null;
		final Item i = Database.instance().itemGet(id, itemClass);
		if (i == null)
			return null;
		return Multimedia.instanceMultimedia(i, id);
	}
	
	/**
	 * Returns the title of the content
	 * @return the title
	 */
	public String getTitle ()
	{
		return this.title;
	}
	
	/**
	 * Returns the URL of the content
	 * @return the URL
	 */
	public String getURL ()
	{
		return this.URL;
	}
	
	/**
	 * Returns the comment for the content
	 * @return the comment
	 */
	public String getComment ()
	{
		return this.comment;
	}
	
	/**
	 * Returns the type for the content
	 * @return the type
	 */
	public String getType ()
	{
		return this.type;
	}
	
	/**
	 * Returns the id for the content
	 * @return the id
	 */
	public String getId ()
	{
		return this.id;
	}
	
	/**
	 * Sets the title
	 * @param title the new title
	 */
	public void setTitle (final String title)
	{
		if (title == null)
			this.title = "";
		else 
			this.title = title;
	}
	
	/**
	 * Sets the comment
	 * @param comment the new comment
	 */
	public void setComment (final String comment)
	{
		if (comment == null)
			this.comment = "";
		else
			this.comment = comment;
	}
	
	/**
	 * Sets the modification status
	 * @param modified the modification flag
	 */
	public void setModified (final boolean modified)
	{
		this.modified = modified;
	}
	
	/**
	 * Returns true if the content is modified or false otherwise
	 * @return the modification status
	 */
	public boolean isModified ()
	{
		return this.modified;
	}
	
	/**
	 * Saves the Multimedia into the database
	 */
	public void save()
	{
		if (!modified)
			return;
		try {
			final ItemManager manager = ItemManager.instance();
			final ItemClass multimediaClass = manager.itemClassGet("MULTIMEDIA");
			final Item multimedia;
			if (id.equals(""))
				multimedia = manager.itemAdd(multimediaClass, -1);
			else
				multimedia = manager.itemGet(id, multimediaClass);
			multimedia.setField("STRING_NAME", title);
			multimedia.setField("STRING_TYPE", type);
			multimedia.setField("STRING_URL", URL);
			multimedia.setField("STRING_COMMENT", comment);
			Database.instance().itemSave(multimedia);
			if (id.equals("")) {
				final String id = Database.instance().getLastInserted("MULTIMEDIA");
				this.id = id;
			}
			this.modified = false;
		}
		catch (final Exception e)
		{
			return;
		}
	}
	
	/**
	 * Deletes the Multimedia from the database
	 */
	public void delete()
	{
		if (id == "")
			return;
		try {
			final Item i = ItemManager.instance().itemAdd(ItemManager.instance().itemClassGet("MULTIMEDIA"), -1);
			i.setId(id);
			Database.instance().itemDelete(i);
		}
		catch (final Exception e)
		{
			return;
		}
	}
	
	/**
	 * Returns a String representing the Multimedia content (it's id or empty String)
	 * @return a string as identifier for the Multimedia
	 */
	public String toString()
	{
		if (id == "")
			return super.toString();
		else
			return id;
	}
}